package com.zmn.plat.admin.controller.part.price;

import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import com.zmn.common.dto2.ResponseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.admin.controller.part.PartBaseController;
import com.zmn.plat.model.entity.part.PartSkuPrice;

/**
 * 类描述： PartSkuPriceListController 配件价格
 *
 * @author DaiZhichao
 * @since 2018/10/23 16:54
 */

@Controller
@RequestMapping("/part/price")
public class PartSkuPriceModifyController extends PartBaseController {

    private Logger logger = LoggerFactory.getLogger(PartSkuPriceModifyController.class);

    /**
     * 描述:配件价格修改Ajax方法
     *
     * @author DaiZhichao
     * @since 2018/11/6 20:39
     */
    // region 修改配件价格详情
    @RequestMapping("/addEdit")
    @ResponseBody
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody PartSkuPrice partSkuPrice) {

        Integer skuId = partSkuPrice.getSkuId();

        if (Objects.isNull(skuId)) {
            return ResponseDTO.fail("修改配件价格失败！skuId不能为空");
        }
        partSkuPrice.setUpdateTime(DateUtil.getNow());
        partSkuPrice.setUpdater(getMcStaffName(request));

        if (Objects.isNull(partSkuPriceService.findByKey(partSkuPrice.getSkuId()))) {
            partSkuPriceService.insert(partSkuPrice, getMcStaff());
            return ResponseDTO.fail("添加配件价格成功！");
        }

        try {
            partSkuPriceService.updateByKey(partSkuPrice, getMcStaff());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            logger.error(String.format("[zmn]新修改配件价格失败,part:[%s],ex:[%s]", JSONObject.toJSONString(partSkuPrice), e.getMessage()));
            return ResponseDTO.fail("修改配件价格失败！");
        }

        return ResponseDTO.success(true,"修改配件价格成功！");
    }
    // endregion

}
